import json

from confluent_kafka import Producer
from Slave.config.slave_config import *
from logger_config import logger_factory

logger = logger_factory()


producer = None


def producer_factory() -> Producer:
    global producer
    if producer is None:
        producer = Producer(slave_producer_conf)
    return producer


def produce_to_queen(mission: str, lot_id: str):
    p = producer_factory()
    info_dict = {'mission': mission, 'lot_id': lot_id, 'slave_id': slave_id}
    try:
        p.produce(slave_to_queen_topic, json.dumps(info_dict))
    except Exception as e:
        logger.error(e)
    p.flush()